package com.zeta.eduservice.controller.file;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.DocumentException;
import com.zeta.eduservice.entity.EduCreateFile;
import com.zeta.eduservice.entity.vo.CreatePaperType;
import com.zeta.eduservice.entity.vo.PaperListVo;
import com.zeta.eduservice.entity.vo.CreatePaperType.WeekDay;
import com.zeta.eduservice.entity.vo.front.ChapterCourseDetialVo;
import com.zeta.eduservice.entity.vo.front.QuestionCreateInfo;
import com.zeta.eduservice.entity.vo.front.front.PaperInfoVo;
import com.zeta.eduservice.entity.yml.TeacherFileYml;
import com.zeta.eduservice.service.EduCreatePdfService;
import com.zeta.eduservice.utils.Download;

@CrossOrigin
@RestController
@RequestMapping("/eduservice/wordMange")
public class EduWordController {
	// 将 yml 中的自定义配置注入到这里
	@Autowired
	private TeacherFileYml teacherFileYml;
	@Autowired
	EduCreatePdfService pdfService;
	@Autowired
	Download downUtil;

//	@PostMapping("create")
//	public void createQuestionWord(
//			@RequestBody String  QuestionTypeList duoyi,
//			HttpServletResponse Response) throws IOException, DocumentException, InvalidFormatException {
//		JSONObject parseObject = JSONObject.parseObject(QuestionTypeList);
//		PaperInfoVo paperInfo = parseObject.getObject("paperInfo",PaperInfoVo.class );
//		List<ChapterCourseDetialVo> chapterList = parseObject.getJSONArray("detailquestioninfoList").toJavaList(ChapterCourseDetialVo.class);
//		Integer integer = parseObject.getInteger("choice");
//
//		List<QuestionCreateInfo> javaList = parseObject.getJSONArray("QuestionTypeList").toJavaList(QuestionCreateInfo.class);
//		EduCreatePdf eduCreatePdf = new EduCreatePdf();
//		eduCreatePdf.setTeaid(paperInfo.getTeaId());
//		eduCreatePdf.setFilename(paperInfo.getPapertitle()+"_"+eduCreatePdf.getId());
//		//eduCreatePdf.setFolderId(paperInfo.getFolderId());
//		eduCreatePdf.setExt("docx");
//		String path=filePath;
//		File file = new File(path);
//		if(!file.exists()){//如果文件夹不存在
//			file.mkdir();//创建文件夹
//		}
//		eduCreatePdf.setPath(path);
//		String pdfDownloadPath = path+"\\"+paperInfo.getTeaId()+"\\"+eduCreatePdf.getFilename()+".docx";
//		File fiel=new  File(pdfDownloadPath);
//		if(!fiel.exists()) {
//			pdfService.createCourseWord(eduCreatePdf,javaList,pdfDownloadPath,paperInfo,chapterList,integer);
//			
//		}
//		downUtil.fileDownLoad(pdfDownloadPath, Response);
//	
//	}

	@PostMapping("createPaperWord")
	public void createPaperQuestionWord(@RequestBody String paperinfo, HttpServletResponse Response)
			throws IOException, DocumentException, InvalidFormatException {
		JSONObject parseObject = JSONObject.parseObject(paperinfo);
		PaperInfoVo paperInfo = parseObject.getObject("paperInfo", PaperInfoVo.class);

		CreatePaperType papercreateType = new CreatePaperType();
		papercreateType.setStatus(paperInfo.getCreateStatus());
		if (papercreateType.getStatus() == 2) {

			EduCreateFile eduCreatePdf = new EduCreateFile();
			eduCreatePdf = pdfService.getById(paperInfo.getPaperId());

			eduCreatePdf.setClasstitle(paperInfo.getClasstitle()).setPapertitle(paperInfo.getPapertitle())
					.setTesttitle(paperInfo.getTesttitle());

			eduCreatePdf.setTeaid(paperInfo.getTeaId());
			eduCreatePdf.setFilename(paperInfo.getPapertitle() + "_" + paperInfo.getPaperId());
			eduCreatePdf.setExt("docx");
			String path = teacherFileYml.getFilePath() + "\\" + paperInfo.getTeaId();
			File file = new File(path);
			if (!file.exists()) {// 如果文件夹不存在
				file.mkdir();// 创建文件夹
			}
			String wordfilePath = path + "\\" + eduCreatePdf.getFilename() + ".docx";
			eduCreatePdf.setPath(wordfilePath);
			pdfService.saveOrUpdate(eduCreatePdf);
			pdfService.createCoursePaperWord(eduCreatePdf, wordfilePath, paperInfo, papercreateType);

			downUtil.fileDownLoad(eduCreatePdf.getPath(), Response);
			File file2 = new File(eduCreatePdf.getPath());
			file2.delete();
		} else if (papercreateType.getStatus() == 3) {

		}
	}

}
